package sort;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

public class Apl {
	public static void main(String[] args) {
		makeReverseSortedFile("test", 20000);		
		int[] array = (readFile("test.txt"));
		
		long begin = System.currentTimeMillis();
		
//		int[] array1 = insertionSorter.sort(array);
		
//		Quicksort quick = new Quicksort();
//		quick.quickSort(array, 0, array.length-1);
		
//		int[] array1 = Quicksort.quickSort(array, 0, array.length-1);
		
		int[] array1 = MergeSort.mergesort(array);
		
		writeLog("Quicksort", array1, begin);
	} 


	public static int[] readFile(String fileName){
		ArrayList<Integer> integerList = new ArrayList<Integer>();
		int size = 0;
		try{
			BufferedReader reader = new BufferedReader(new FileReader( new File(fileName) ) );
			while(reader.ready()){
				integerList.add( (Integer.parseInt( reader.readLine() ) ) );
				size++;
			}
			int[] integer = new int[size];
			for(int i = 0; i < size; i++){
				integer[i] = integerList.get(i);			
			}
			return integer;
		}
		catch(IOException e){
			e.printStackTrace();
			return new int[0];
		}

			
	}

	public static void makeRandomFile(String fileName, int size){
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter( new File(fileName + ".txt") ) );
			for(int i = 0; i < size; i++){
				writer.write((int) ( Math.random() * size  +1 ) + "");	
				if(size-i > 1)
					writer.newLine();
			}
			writer.close();
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void makeSortedFile(String fileName, int size){
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter( new File(fileName + ".txt") ) );
			for(int i = 0; i < size; i++){
				writer.write(i + "");	
				if(size-i > 1)
					writer.newLine();
			}
			writer.close();
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void makeReverseSortedFile(String fileName, int size){
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter( new File(fileName + ".txt") ) );
			for(int i = 0; i < size; i++){
				writer.write(size-i + "");	
				if(size-i > 1)
					writer.newLine();
			}
			writer.close();
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void makeDuplicateFile(String fileName, int size){
		Random r = new Random();
		int digit = 0;
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter( new File(fileName + ".txt") ) );
			for(int i = 0; i < size; i++){
				int j = r.nextInt(3);
				if(j == 0)
					digit = 100;
				if(j == 1)
					digit = 200;
				if(j == 2)
					digit = 300;
				writer.write(digit + "");	
				if(size-i > 1)
					writer.newLine();
			}

			writer.close();
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	
	private static void writeLog(String method, int[] array, long begin){
		try {
			long end = System.currentTimeMillis();
			
			String fileName = method
					+ Calendar.getInstance().get(Calendar.YEAR) + "-"
					+ Calendar.getInstance().get(Calendar.MONTH) + "-"
					+ (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + 1)
					+ "-" + Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
					+ "=" + Calendar.getInstance().get(Calendar.MINUTE);

			BufferedWriter writer = new BufferedWriter(new FileWriter( new File(fileName + ".txt") ) );
			
			writeHead(method, begin, end, writer);
			writeArray(array, writer);	
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	private static void writeArray(int[] array, BufferedWriter writer)
			throws IOException {
		for(int i = 0; i < array.length; i++){
			writer.write(array[i] + "");	
			if(array.length - i > 1){
				writer.newLine();
			}
		}
	}


	private static void writeHead(String method, long begin, long end,
			BufferedWriter writer) throws IOException {
		writer.write("method: " + method + "\n" );
		writer.write("============================\n");
		writer.write("beginTime: " + begin + "\n" );
		writer.write("============================\n");
		writer.write("endTime: " + end + "\n" );
		writer.write("============================\n");
		writer.write("time collapsed: " + (end-begin) + "ms\n" );
		writer.write("time collapsed: " + ( (end-begin) / 1000) + "s\n" );
		writer.write("time collapsed: " + ( (end-begin) /1000 / 60 ) + "min\n" );
		writer.write("============================\n");
	}
	
}
